# 需求：爬取京东一个商品的评论
import requests
import json

# 不同商品的基础路径可能不同，还有个sclub开头的。
comment_url = 'https://club.jd.com/comment/productPageComments.action'

params = {
    'productId': 100009077475,  # 商品id。先写死 苹果手机。
    'score': 0,
    'sortType': 5,
    'page': 0,
    'pageSize': 10,
    # 'callback': 'fetchJSON_comment98',  # 不加返回完美json
    'isShadowSku': 0,
    'fold': 1,
    'rid': 0
}

# 伪造headers。 经测试club开头的域名需要cookie。
headers = {
    'referer': 'https://item.jd.com/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'cookie': '__jdu=1599471405142969161485; shshshfpa=7aa99a93-8740-8347-7a36-9796bf8a56f1-1599471406; shshshfpb=rZoE4ylDovn%204UYgNS8j1vg%3D%3D; pinId=uGywawhW5xQ; pin=zzdxyz; unick=%E7%BB%86%E8%BD%AF%E8%B7%91; _tp=EtAWqzuy%2FaNSh0LFcTfGgQ%3D%3D; _pst=zzdxyz; cn=0; __jdv=76161171|direct|-|none|-|1606737681370; areaId=7; ipLoc-djd=7-412-3546-0; PCSYCityID=CN_410000_410100_410104; TrackID=15-vsa5KNp37GRSsGyDw3_WgLbFNcS-cKjulnm5KayEhOB3crMdyeuKtvOsoxMoS9g-6XERHFcQM7wC8u8bPtQ9A9NiXmzHzDjqAQR3qGyM8ePE0V2KktAWJNlAycKZix; ceshi3.com=000; user-key=48887e42-929f-42af-8a1c-641fd968ad8c; __jda=122270672.1599471405142969161485.1599471405.1606737681.1606959119.13; __jdc=122270672; shshshfp=ea5f09a7b02898b86d5dff6cb7b0c445; jwotest_product=99; 3AB9D23F7A4B3C9B=F3EUR3KATWFXC6MXLNZM5BDVXQWGCRYREFQAPSKEW6DWCLX4QBKAZUW5MDFCHUDYNAAQVH4AYTB7NKS2IBR5ADYIBA; wlfstk_smdl=s659yndia19zvljgih2ifqoneu7krpqj; JSESSIONID=224ABEFBB0C271ADF44C4F7821C5B90E.s1'
}

comment_resp = requests.get(url=comment_url, params=params, headers=headers)
print(comment_resp.status_code)
comment_str = comment_resp.text

comment_dict = json.loads(comment_str)
# comment_dict = json.load('4js接口-京东评论-评论数据.json')
comments = comment_dict['comments']
for comment in comments:
    print(comment['id'])
    print(comment['content'])
    print(comment['creationTime'])
    print(comment['score'])
    print(comment['nickname'])
    images = comment['images']
    for image in images:
        print(image['imgUrl'])
    print('-'*50)







